export const upurl="https://www.zhaoio.cn/api/common/upload";
export const baseUrl="https://www.zhaoio.cn";
export default function upload(files={}){
  console.log(files)
  return new Promise((resolve,reject)=>{
    wx.uploadFile({
      url:upurl,
      filePath:files.files.url,
      name: 'file',
      header:{
        'content-type': 'application/x-www-form-urlencoded',
        'token': wx.getStorageSync('utoken')?wx.getStorageSync('utoken'):"",
      },
      success:(res)=>{
        if(res.statusCode==401){
          //token 失效重新执行登录
          wx.login({
            success:function(res){
              wx.request({
                url:baseUrl+'/api/user/ulogin',
                data:{code:res.code},
                method:'POST',
                success:function(ret){
                  wx.setStorageSync('utoken', ret.data.data.userinfo.token);
                  wx.showToast({
                    title:'再试一次',
                    icon: 'none',
                    duration: 2000
                  })
                }
              })
            }
          })
        }else{
          //不需要鉴权或者鉴权成功 直接返回
          resolve(res.data)
        }
      },
      fail:(res)=>{
        reject('服务器异常，请稍后重试！')
      }
    })
  })

}